﻿using _1._11._2015_BookReview.Adapter;
using _1._11._2015_BookReview.Data;
using _1._11._2015_BookReview.Data.Models;
using _1._11._2015_BookReview.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _1._11._2015_BookReview.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        private IBookReviewAdapter _adapter;

        public HomeController()
        {
            _adapter = BookReviewAdapterFactory.GetBookReviewAdapter();
        }

        public HomeController(IBookReviewAdapter adapter)
        {
            _adapter = adapter;
        }

        public ActionResult Index()
        {
            BooksViewModel model = new BooksViewModel(); 
            model.Books = _adapter.GetBooksViewModel();
            return View(model);
        }

        public ActionResult CreateBook()
        {
            BookViewModel model = new BookViewModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult CreateBook(BookViewModel model)
        {
            _adapter.AddBook(model);
            return RedirectToAction("Index");
        }

        public ActionResult Review(int id)
        {

            ReviewViewModel model = _adapter.GetBlankReview(id); // blank review will contain book info
            return View(model);
        }

        [HttpPost]
        public ActionResult Review(ReviewViewModel model, int bookId)
        {


            string userId = User.Identity.GetUserId();
            _adapter.AddReview(model, userId, bookId);
            return RedirectToAction("Detail", new {id = bookId});

        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult Detail(int id)
        {
            BookViewModel model = _adapter.GetBookViewModel(id);
            return View(model);
        }

    }
}